package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorCommonServiceDownNew;
import guoanmaker.operator.business.model.OperatorCommonServiceNew;
import guoanmaker.operator.business.model.OperatorCommonServiceNewRound;
import guoanmaker.operator.business.repository.OperatorCommonServiceDownNewRepository;
import guoanmaker.operator.business.repository.OperatorCommonServiceNewRepository;
/**
 * 
    * @ClassName: OperatorCommonServiceNewService  
    * @Description: 运营后台PC首页控制常用服务配置接口  
    * @author lsy  
    * @date 2017年12月15日 上午11:36:10    
    *
 */

@Service
public class OperatorCommonServiceNewService implements OperatorCommonServiceNewServiceInterface {
	@Resource
	private OperatorCommonServiceNewRepository operatorCommonServiceNewRepository;
	@Resource
	private OperatorCommonServiceDownNewRepository operatorCommonServiceDownNewRepository;
	
	@PersistenceContext
	private EntityManager manager;
	
	/**
	 * 
	    * @Title: saveOperatorCommonServiceNew  
	    * @Description: 常用服务配置  
	    * @param @param inRoundPhone
	    * @throws
	 */
	@Override
	@Transactional
	public void saveOperatorCommonServiceNew(InRoundPhone inRoundPhone) {
		OperatorCommonServiceNewRound[] operatorCommonServiceNewRounds = inRoundPhone.getOperatorCommonServiceNewRound();
		for (OperatorCommonServiceNewRound operatorCommonServiceNewRound : operatorCommonServiceNewRounds) {
			OperatorCommonServiceNew operatorCommonServiceNew= operatorCommonServiceNewRound.getOperatorCommonServiceNew();
			operatorCommonServiceNew.setCityCode(inRoundPhone.getCityId());
			operatorCommonServiceNew =operatorCommonServiceNewRepository.save(operatorCommonServiceNew);
			
			List<OperatorCommonServiceDownNew> commonServiceDownNews = operatorCommonServiceNewRound.getOperatorCommonServiceDownNew();
			
			for (OperatorCommonServiceDownNew operatorCommonServiceDownNew : commonServiceDownNews) {
				operatorCommonServiceDownNew.setCommonServiceNewId(operatorCommonServiceNew.getId());
				
				operatorCommonServiceDownNewRepository.save(operatorCommonServiceDownNew);
			}
		}

	
	}
	
	/**
	 * 
	    * @Title: selectAllOperatorCommonServiceNew  
	    * @Description: 传入城市id 查询所有服务配置 
	    * @param @param cityCode
	    * @return OperatorCommonServiceNewRound      
	    * @throws
	 */
	@Override
	
	public List<OperatorCommonServiceNewRound> selectAllOperatorCommonServiceNew(String cityCode) {
		List<OperatorCommonServiceNewRound> operatorCommonServiceNewRounds = new ArrayList<>();
		List<OperatorCommonServiceNew> operatorCommonServiceNews = operatorCommonServiceNewRepository.selectCommonServiceBycityCode(cityCode);//commonServiceQuery.getResultList();
		//根据OperatorCommonServiceNew 集合去查询 OperatorCommonServiceDownNew
		for (OperatorCommonServiceNew operatorCommonServiceNew : operatorCommonServiceNews) {
			List<OperatorCommonServiceDownNew> operatorCommonServiceDownNews = operatorCommonServiceDownNewRepository.findCommonServiceDownByCommonServiceNewId(operatorCommonServiceNew.getId());
			OperatorCommonServiceNewRound operatorCommonServiceNewRound= new OperatorCommonServiceNewRound();
			operatorCommonServiceNewRound.setOperatorCommonServiceNew(operatorCommonServiceNew);
			operatorCommonServiceNewRound.setOperatorCommonServiceDownNew(operatorCommonServiceDownNews);
			operatorCommonServiceNewRounds.add(operatorCommonServiceNewRound);
			
		}
		
		return operatorCommonServiceNewRounds;
	}
	
	/**
	 * * @Title: deleteOperatorCommonServiceNewRound  
	    * @Description: 删除常用服务准备从新插入 
	    * @param @param inRoundPhone 中的cityId
	    * @return AjaxJson      
	    * @throws
	 */
	@Override
	@Transactional
	public void deleteOperatorCommonServiceNewRound(InRoundPhone inRoundPhone) {
	
		String jpql = "delete from t_operator_common_service_new  where city_code='"+inRoundPhone.getCityId()+"'";
		Query query = manager.createNativeQuery(jpql);
		query.executeUpdate();
		
	}

}
